﻿(function () {
    var modulePlaceholderDialog = function (editor) {
        return {
            title: 'Module Placeholder Dialog',
            minWidth: 300,
            minHeight: 300,
            buttons: [CKEDITOR.dialog.okButton, CKEDITOR.dialog.cancelButton],
            onOk: function () {
                if (this.getValueOf('selectModule', 'ddlModules') != '') {
                    var elem = editor.document.createElement('div');
                    elem.setAttribute('style', 'float:' + this.getValueOf('selectModule', 'ddlAlign') + ';');
                    elem.setAttribute('class', 'moduleplaceholder ' + this.getValueOf('selectModule', 'ddlModules'));
                    var newFakeImage = editor.createFakeElement(elem, 'moduleplaceholder', 'moduleplaceholder', true);
                    newFakeImage.setAttribute('align', this.getValueOf('selectModule', 'ddlAlign'));
                    if (this.fakeImage) {
                        newFakeImage.replace(this.fakeImage);
                        editor.getSelection().selectElement(newFakeImage);
                    }
                    else editor.insertElement(newFakeImage);
                }
            },
            onShow: function () {
                var fakeElem = this.getSelectedElement();
                if (fakeElem != null) {
                    this.fakeImage = fakeElem;
                    var realElem = editor.restoreRealElement(fakeElem);
                    this.setupContent(realElem);
                }
            },
            resizable: CKEDITOR.DIALOG_RESIZE_BOTH,
            contents: [{
                id: 'selectModule',
                label: 'Select Module',
                title: 'Select Module',
                accessKey: 'S',
                elements: [{
                    type: 'select',
                    label: 'Alignment: ',
                    labelLayout: 'horizontal',
                    id: 'ddlAlign',
                    items: [['Not Set', 'none'], ['Left', 'left'], ['Right', 'right']],
                    widths: ['50%', '50%'],
                    style: 'width:65px;',
                    setup: function (elem) {
                        if (elem != null) this.setValue(elem.getStyle('float'));
                    }
                }, {
                    type: 'select',
                    label: 'Modules: ',
                    id: 'ddlModules',
                    items: (function () {
                        var strJSON = CKEDITOR.ajax.load('/lidocaine.ashx?f=LoadModules');
                        var oJSON = eval("(" + strJSON + ")");
                        var arrayModules = new Array(oJSON.length + 1);
                        var arrayModule = new Array(2);
                        arrayModule[0] = '';
                        arrayModule[1] = '';
                        arrayModules[0] = arrayModule.slice();
                        for (var i = 0; i < oJSON.length; i++) {
                            arrayModule[0] = oJSON[i].ModuleName;
                            arrayModule[1] = oJSON[i].ModuleID;
                            arrayModules[i + 1] = arrayModule.slice();
                        }
                        return arrayModules;
                    })(),
                    style: 'width:100%;',
                    setup: function (elem) {
                        if (elem != null) this.setValue(elem.getAttribute('class').substring(elem.getAttribute('class').indexOf(' ') + 1));
                    },
                    onChange: function () {
                        if (this.getValue() != "") this.getDialog().setValueOf('selectModule', 'txtDescription', CKEDITOR.ajax.load('/lidocaine.ashx?f=LoadModuleDescription&ID=' + this.getValue()));
                    }
                }, {
                    type: 'textarea',
                    id: 'txtDescription',
                    label: 'Module Description:',
                    rows: 10,
                    setup: function (elem) {
                        if (elem != null) { this.setValue(CKEDITOR.ajax.load('/lidocaine.ashx?f=LoadModuleDescription&ID=' + elem.getAttribute('class').substring(elem.getAttribute('class').indexOf(' ') + 1))); }
                    }
                }]
            }]
        };
    };
    CKEDITOR.dialog.add('moduleplaceholder', function (editor) { return modulePlaceholderDialog(editor); });
})();